from OpenGL.GL import *
import pyglet

from common import *
from game.core.gamemanager import GameManager

class ControlBar(object):
	"""
	This class represents the bottom control bar.
	"""
	
	ATACK = 0
	MOVE = 1
	CONSTRUCT = 2
	RESOURCES = 3
	WORKER = 4
	WARRIOR = 5
	
	def __init__(self):
		# Creates the logo sprite
		img = pyglet.image.load('../data/images/control/bg_metal.gif')
		self.base = pyglet.sprite.Sprite(img,
					x=GameManager.main_window.win.width / 2, y=0)
	
		# Creates the menu items
		self.items = []
		img = pyglet.image.load('../data/images/control/btn_attack.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered = pyglet.image.load('../data/images/control/btn_attack_hovered.png')
		img_hovered.anchor_x = img.width / 2
		img_hovered.anchor_y = img.height / 2
		self.items.append(MenuItem(50, 100, img, ControlBar.ATACK, img_hovered, img))
		img = pyglet.image.load('../data/images/control/btn_move.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered = pyglet.image.load('../data/images/control/btn_move_hovered.png')
		img_hovered.anchor_x = img.width / 2
		img_hovered.anchor_y = img.height / 2
		self.items.append(MenuItem(140, 100, img, ControlBar.MOVE, img_hovered, img))
		img = pyglet.image.load('../data/images/control/btn_construct.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered = pyglet.image.load('../data/images/control/btn_construct_hovered.png')
		img_hovered.anchor_x = img.width / 2
		img_hovered.anchor_y = img.height / 2
		self.items.append(MenuItem(230, 100, img, ControlBar.CONSTRUCT, img_hovered, img))
		img = pyglet.image.load('../data/images/control/btn_resources.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered = pyglet.image.load('../data/images/control/btn_resources_hovered.png')
		img_hovered.anchor_x = img.width / 2
		img_hovered.anchor_y = img.height / 2
		self.items.append(MenuItem(320, 100, img, ControlBar.RESOURCES, img_hovered, img))
		img = pyglet.image.load('../data/images/control/nanwarrior.png')
		img_hovered = pyglet.image.load('../data/images/control/nanwarrior_hovered.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered.anchor_x = img_hovered.width / 2
		img_hovered.anchor_y = img_hovered.height / 2
		self.items.append(MenuItem(1100, 130, img, ControlBar.WORKER, img_hovered, img))
		img = pyglet.image.load('../data/images/control/nanwarrior_2.png')
		img.anchor_x = img.width / 2
		img.anchor_y = img.height / 2
		img_hovered = pyglet.image.load('../data/images/control/nanwarrior_hovered.png')
		img_hovered.anchor_x = img.width / 2
		img_hovered.anchor_y = img.height / 2
		self.items.append(MenuItem(1180, 130, img, ControlBar.WARRIOR, img_hovered, img))
	
		self.hovered = None
		self.clicked = False

	def render(self):
		glDisable(GL_LIGHTING)
		repeat_num = GameManager.main_window.win.width / self.base.image.width + 1
		for i in range(repeat_num):
			self.base.x = i * self.base.image.width
			self.base.draw()
		for i in self.items:
			i.render()
		glEnable(GL_LIGHTING)
	
	def on_mouse_press(self, x, y, button, modifiers):
		if not(self.hovered is None):
			self.hovered.status = MenuItem.CLICKED
		self.clicked = True
		GameManager.play_sound('menu_click')
				
	def on_mouse_motion(self, x, y, dx, dy):
		if not(self.clicked is None):
			self.clicked = None
			
		if not(self.hovered is None):
			if self.hovered.isInside(x, y):
				return
			else:
				self.hovered.status = MenuItem.UNSELECTED
				self.hovered = None
		
		for i in self.items:
			if i.isInside(x, y) and i.status == MenuItem.UNSELECTED:
				if self.clicked:
					i.status = MenuItem.CLICKED
				else:
					i.status = MenuItem.HOVERED
				self.hovered = i
				break

		
class MenuItem(object):
	"""
	This class represents a main menu item. 
	"""
	
	# Possible menu item status.
	UNSELECTED = 0
	HOVERED = 1
	CLICKED = 2
	DISABLED = 3
	
	img_button_base = pyglet.image.load('../data/images/control/btn_base.png')
	img_button_base.anchor_x = img_button_base.width / 2
	img_button_base.anchor_y = img_button_base.height / 2
	
	def __init__(self, x, y, img_label, id, img_hovered = None, img_clicked = None):
		imgSize = [MenuItem.img_button_base.width, MenuItem.img_button_base.height]
		self.status = MenuItem.UNSELECTED
		
		self.img_hovered = img_hovered
		self.img_clicked = img_clicked
		
		self.hovered_sprite = None
		self.clicked_sprite = None
		 
		if (img_hovered is not None):
			self.hovered_sprite = pyglet.sprite.Sprite(img_hovered, x, y)
		if (img_clicked is not None):
			self.clicked_sprite = pyglet.sprite.Sprite(img_clicked, x, y)

		self.label = pyglet.sprite.Sprite(img_label, x, y)
		self.boundings = [x - imgSize[X] / 2, y - imgSize[Y] / 2,
				x + imgSize[X] / 2, y + imgSize[Y] / 2]
		
		self.id = id
		
	def render(self):
		if self.status == MenuItem.UNSELECTED:
			self.label.draw()
		elif self.status == MenuItem.HOVERED:
			if self.hovered_sprite is not None:
				self.hovered_sprite.draw()
		elif self.status == MenuItem.CLICKED:
			if self.clicked_sprite is not None:
				self.clicked_sprite.draw()
			
	def isInside(self, x, y):
		"""
		States if the point (x,y) is over the button. 
		"""
		
		if x > self.boundings[0] and x < self.boundings[2] and \
			y > self.boundings[1] and y < self.boundings[3]:

			return True

		return False
	